package com.ruoyi.system.service.constructionLike.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.constructionLike.ConstructionRemark;
import com.ruoyi.system.domain.constructionLike.ConstructionTags;
import com.ruoyi.system.domain.vo.ConstructionCustomerVo;
import com.ruoyi.system.mapper.constructionLike.ConstructionRemarkMapper;
import com.ruoyi.system.mapper.constructionLike.ConstructionTagsMapper;
import com.ruoyi.system.service.constructionLike.ConstructionRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 施工攻略Service业务层处理
 *
 * @author gxj
 * @date 2021-04-11
 */
@Service
public class ConstructionRemarkServiceImpl implements ConstructionRemarkService {
    @Autowired
    private ConstructionRemarkMapper constructionRemarkMapper;
    @Autowired
    private ConstructionTagsMapper constructionTagsMapper;

    /**
     * 查询施工攻略
     *
     * @param id 施工攻略ID
     * @return 施工攻略
     */
    @Override
    public ConstructionRemark selectConstructionRemarkById(Integer id)
    {
        return constructionRemarkMapper.selectConstructionRemarkById(id);
    }

    /**
     * 查询施工攻略列表
     *
     * @param constructionRemark 施工攻略
     * @return 施工攻略
     */
    @Override
    public List<ConstructionCustomerVo> selectConstructionRemarkList(ConstructionCustomerVo constructionRemark)
    {
        return constructionRemarkMapper.selectConstructionRemarkList(constructionRemark);
    }

    @Override
    public List<ConstructionCustomerVo> findAllList(ConstructionCustomerVo constructionRemark) {


        /*List<ConstructionCustomerVo> constructionList = new ArrayList<>();
        ConstructionCustomerVo constructionRemark1 = new ConstructionCustomerVo();
        constructionRemark1.setConstructionStrategyId(15);
        List<ConstructionCustomerVo> rootData = constructionRemarkMapper.selectConstructionRemarkList(constructionRemark1);



        ConstructionCustomerVo constructionRemark2 = new ConstructionCustomerVo();
        constructionRemark2.setConstructionStrategyId(15);*/


        /*List<ConstructionCustomerVo> byIdRemark = */


        /*for (ConstructionCustomerVo list : byIdRemark){
            Integer remarkId = list.getRemarkId();
//            if (remarkId != 0){
//                System.err.println("没有下级");
//            }else{
                constructionList.add(list);
//            }
        }

        for (ConstructionCustomerVo list : constructionList){
            Integer id = list.getId();
            list.setTreeList(getxiaji(id,rootData));
        }*/



        return constructionRemarkMapper.findByIdRemark(constructionRemark);
    }

    @Transactional
    public List<ConstructionCustomerVo> getxiaji(Integer id,List<ConstructionCustomerVo> rootData){
        List<ConstructionCustomerVo> childList = new ArrayList<>();

        for (ConstructionCustomerVo list : rootData){
            if (list.getRemarkId() == 0){
                System.err.println("没有上级");
            }else if(list.getRemarkId() == id){
                childList.add(list);
            }
        }


        for (ConstructionCustomerVo list : childList){
            Integer id1 = list.getId();
            list.setTreeList(getxiaji(id1,rootData));
        }

        if (childList.size() == 0){
            return null;
        }

        return childList;
    }














    /**
     * 新增施工攻略
     *
     * @param constructionRemark 施工攻略
     * @return 结果
     */
    @Override
    public int insertConstructionRemark(ConstructionRemark constructionRemark)
    {
        constructionRemark.setCreateTime(DateUtils.getNowDate());
        ConstructionTags constructionTags = new ConstructionTags();
        constructionTags.setUserId(constructionRemark.getUserId());
        constructionTags.setConstructionStrategyId(constructionRemark.getConstructionStrategyId());
        ConstructionTags constructionTags1 = constructionTagsMapper.selectConstructionTagsList(constructionTags);
        if(constructionTags1 != null && !constructionTags1.equals("")){
            constructionRemark.setTagsId(Math.toIntExact(constructionTags1.getId()));
            int i = constructionRemarkMapper.insertConstructionRemark(constructionRemark);
            if (i > 0){
                return 1;
            }
        }else{
            int i = constructionTagsMapper.insertConstructionTags(constructionTags);
            constructionRemark.setTagsId(Math.toIntExact(constructionTags.getId()));
            if (i > 0) {
                int i1 = constructionRemarkMapper.insertConstructionRemark(constructionRemark);
                if (i1 > 0){
                    return 1;
                }
            }
        }
        return 0;
    }

    /**
     * 修改施工攻略
     *
     * @param constructionRemark 施工攻略
     * @return 结果
     */
    @Override
    public int updateConstructionRemark(ConstructionRemark constructionRemark)
    {
        return constructionRemarkMapper.updateConstructionRemark(constructionRemark);
    }

    /**
     * 批量删除施工攻略
     *
     * @param ids 需要删除的施工攻略ID
     * @return 结果
     */
    @Override
    public int deleteConstructionRemarkByIds(Integer[] ids)
    {
        return constructionRemarkMapper.deleteConstructionRemarkByIds(ids);
    }

    /**
     * 删除施工攻略信息
     *
     * @param id 施工攻略ID
     * @return 结果
     */
    @Override
    public int deleteConstructionRemarkById(Integer id)
    {
        return constructionRemarkMapper.deleteConstructionRemarkById(id);
    }
}
